// 主界面
;define(function(require, exports, module) {
    'use strict';
    var util = require("util");
    var base = require("base");
    var leftMenu = require("../leftMenu/leftMenu");
    var topMenu = require("../topMenu/topMenu");
    var router = require("../hash/pageRouter");
    var footer = require("../footer/footer_main");
    var settlementOverdue_css = require("./settlementOverdue.css");
    var dialog = require("dialog");
    var validate = require("validate");
    var constant = require("constant");
    var databind = require("databind");
    var firstVisited = 0;
    var tools = require("tools");
    var auditState;
    var ym;
    var leftM;
    var pageIndex = 1,
        pageSize = 10,
        totalPageCount = 1,
        totalRecordCount = 0; //分页初始化
        
    var init = function(){
    	leftM = auditState = base.getUrlParam("leftM");
    	leftM = leftM == null?4:leftM;
    	footer.init();  	
    	$("#footer").removeClass("none");

    	leftMenu.init(leftM);
    	
    	$(".serviceFeeIncome").parents("li.leftMenu-item").addClass("leftMenu-open");
    	$(".leftMenu-item").removeClass("leftMenu-active");
    	$(".settlementOverdue").addClass("leftMenu-active");
    	
    	topMenu.init(2);
    	$(".footer").removeClass("none");
    	
    	inquiryInfo();
		initEvent();
		router.init();
		
		if(firstVisited != 1){
			$(".table-section").removeClass("none");
		}
    };
    
	var initEvent = function(){
		// 点击出账账单
		$("#settlementOverdue").on("click", ".billInfo", function() {
			$(this).css("color", "red");
			$("#settlementOverdue .searchInfo").css("color", "black");
			$(".panel2_left").css("width", "1%");
			$(".panel2_right").css("width", "89%");
			
			$("#settlementOverdue .billInfoDiv").show();
			$("#settlementOverdue .tableSearch").hide();
		})
		
		// 点击查询
		$("#settlementOverdue").on("click", ".searchInfo", function() {
			$(this).css("color", "red");
			$("#settlementOverdue .billInfo").css("color", "black");
			$(".panel2_left").css("width", "18%");
			$(".panel2_right").css("width", "72%");
			
			$("#settlementOverdue .billInfoDiv").hide();
			$("#settlementOverdue .tableSearch").show();
		})
		
		// 详情
		$("#settlementOverdue").on("click", ".table_details_link", function() {
			var ym = $(this).parent("tr.tr_style").attr("yearMonth");
			router.route("client/settlementOverdueDetail.html?ym=" + ym,true);
		});
		
		// 向前
		$("#settlementOverdue").on("click", ".forward", function() {
			forwardOrBackward(true);
		})
		
		// 向后
		$("#settlementOverdue").on("click", ".backward", function() {
			forwardOrBackward(false);
		})
    };
    
    var inquiryInfo = function() {
    	// 获取当前年月
    	var dateNow = new Date();
    	var y = dateNow.getFullYear();
    	var m = dateNow.getMonth() + 1;
    	var d = dateNow.getDate();
    	if(d > 20) {
    		m += 1;
    	}
    	if(m < 10) {
    		m = "0" + m;
    	}
    	ym= y + "-" + m;
    	$("#settlementOverdue .yearMonth").text(ym);
    	
    	/*base.fnAjax({
			action: ".json",
			async: false,
			nocode: true,
			oData: {
				pageIndex: pageIndex,
                pageSize: pageSize
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					
				}else {
					//
				}
			}
		})*/
    };
    
    //向前或向后
    var forwardOrBackward = function(forward) {
    	var yearLater = undefined;
    	var monthLater = undefined;
    	var yearMonthLater = undefined;
    	var yearMonthCurrent = $("#settlementOverdue .yearMonth").text();
    	var yearMonth = yearMonthCurrent.split("-");
    	var year = parseFloat(yearMonth[0]);
    	var month = parseFloat(yearMonth[1]);
    	if(forward == true) { //向前
    		month = month + 1;
    		if(month > 0 && month < 10) {
    			monthLater = "0" + month;
    			yearLater = year;
    		}else if(month >= 10 && month <= 12) {
    			monthLater = month;
    			yearLater = year;
    		}else if(month > 12) {
    			monthLater = "01";
    			yearLater = year + 1;
    		}
    	}else { // 向后
    		month = month - 1;
    		if(month <= 0) {
    			monthLater = "12"
    			yearLater = year - 1;
    		}else if(month > 0 && month < 10) {
    			monthLater = "0" + month;
    			yearLater = year;
    		}else if(month >= 10 && month <= 12) {
    			monthLater = month;
    			yearLater = year;
    		}else if(month > 12) {
    			monthLater = "01";
    			yearLater = year - 1;
    		}
    	}
    	yearMonthLater = yearLater + "-" + monthLater;
    	$("#settlementOverdue .yearMonth").text(yearMonthLater);
    };
    
    return {
        init:init
    };
});